//
//  CouseSeachListView.m
//  caishuitong
//
//  Created by 企友软mac on 2017/5/27.
//  Copyright © 2017年 zhangxy. All rights reserved.
//

#import "CouseSeachListView.h"
#import "XDMultTableView.h"

#define DiaWinWidth [UIScreen mainScreen].bounds.size.width
#define DiaWinHeight [UIScreen mainScreen].bounds.size.height
#define WRandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]

@interface CouseSeachListView()<XDMultTableViewDatasource,XDMultTableViewDelegate>{
    NSInteger flfs;
    NSInteger dj;
}

@property(nonatomic, readwrite, strong)XDMultTableView *tableView;

@property (nonatomic, strong)NSMutableArray *arrHead;

@end

@implementation CouseSeachListView

- (NSMutableArray *)arrHead {
    if (_arrHead == nil) {
        _arrHead = [NSMutableArray array];
    }
    return _arrHead;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        NSArray *arr = [WShareUtil sharedData].arrList;
        [self.arrHead addObjectsFromArray:arr];
        
//        //去除会员
//        NSRange range;
//        for (NSDictionary *dic in arr) {
//            if (![WShareUtil strNilOrEmpty:dic[@"value"]]){
//                range = [dic[@"value"] rangeOfString:@"会员"];
//                if (range.location != NSNotFound) {
//                    [self.arrHead removeObject:dic];
//                }
//            }
//        }
        
//        NSDictionary *dic = @{@"children":@[@{@"key":@"click",@"value":@"热门"},@{@"key":@"createTime",@"value":@"最新"},@{@"key":@"createTime",@"value":@"最好"},@{@"key":@"createTime",@"value":@"最差"},@{@"key":@"createTime",@"value":@"最棒"}],@"key":@"px",@"value":@"排序方式"};
//
        [self arrHead];
//        [self.arrHead addObject:dic];
        WLog(@"%ld",self.arrHead.count);
        
        _tableView = [[XDMultTableView alloc] initWithFrame:frame];
        _tableView.openSectionArray = [NSArray arrayWithObjects:@0,@1,@2,@3,@4,@5,@6,nil];
        _tableView.delegate = self;
        _tableView.datasource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.autoAdjustOpenAndClose = NO;
        _tableView.tableView.bounces = NO;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addToucheEvent:)];
        [_tableView.tableView addGestureRecognizer:tap];
        
        UIView *bgView = [[UIView alloc]initWithFrame:frame];
        bgView.userInteractionEnabled = YES;
        bgView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
        bgView.alpha = 0.6;
        [self addSubview:bgView];
        [self addSubview:_tableView];
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)addToucheEvent:(UITapGestureRecognizer *)tap
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(btnOfDisappearClicked)]) {
        [self.delegate btnOfDisappearClicked];
    }
}

#pragma mark - datasource
- (NSInteger)mTableView:(XDMultTableView *)mTableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (XDMultTableViewCell *)mTableView:(XDMultTableView *)mTableView
              cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"cell";
    
    UITableViewCell  *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    
    UIView *view = [self createGridView:indexPath];
    [cell addSubview:view];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(XDMultTableView *)mTableView{
    return self.arrHead.count;
}

-(NSString *)mTableView:(XDMultTableView *)mTableView titleForHeaderInSection:(NSInteger)section{
    
    NSDictionary *dic = self.arrHead[section];
    return dic[@"value"];
}

#pragma mark - delegate
- (CGFloat)mTableView:(XDMultTableView *)mTableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger btnCount = [self.arrHead[indexPath.section][@"children"] count];
    
    NSInteger rowCount = 0;
    if (btnCount > 0 && btnCount % 4 != 0) {
        rowCount = btnCount / 4 + 1;
    }else if (btnCount > 0 && btnCount % 4 == 0) {
        rowCount = btnCount / 4;
    }
    return 53 * rowCount;
}

- (CGFloat)mTableView:(XDMultTableView *)mTableView heightForHeaderInSection:(NSInteger)section{
    return 42;
}


- (void)mTableView:(XDMultTableView *)mTableView willOpenHeaderAtSection:(NSInteger)section{
    WLog(@"即将展开");
}


- (void)mTableView:(XDMultTableView *)mTableView willCloseHeaderAtSection:(NSInteger)section{
    WLog(@"即将关闭");
}

- (void)mTableView:(XDMultTableView *)mTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WLog(@"点击cell");
}

// 创建
- (UIView *)createGridView:(NSIndexPath *)indexPath
{
    NSInteger btnCount = [self.arrHead[indexPath.section][@"children"] count];
    NSArray *arrTitle = self.arrHead[indexPath.section][@"children"];
    
    NSInteger rowCount = 0;
    if (btnCount > 0 && btnCount % 4 != 0) {
        rowCount = btnCount / 4 + 1;
    }else if (btnCount > 0 && btnCount % 4 == 0) {
        rowCount = btnCount / 4;
    }
    
    UIView *gridView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 50 * rowCount)];
    gridView.backgroundColor = [UIColor whiteColor];
    
    for (int i = 0; i < rowCount + 1; i++) {
        UILabel *lineHorizontal = [[UILabel alloc]initWithFrame:CGRectMake(0, 44 * i, WSCREENH_WIDTH, 0.5)];
//        lineHorizontal.backgroundColor = WRGBColor(234, 234, 234);
        lineHorizontal.backgroundColor = [UIColor clearColor];
        [gridView addSubview:lineHorizontal];
    }
    
    for (int i = 1; i <= 2; i++) {
        UILabel *lineVertical = [[UILabel alloc]initWithFrame:CGRectMake((WSCREENH_WIDTH - 2)/4. * i, 5, 0.5, gridView.bounds.size.height - 10)];
        
        WLog(@"%f",(WSCREENH_WIDTH - 2)/4. * i);
//        lineVertical.backgroundColor = WRGBColor(234, 234, 234);
        lineVertical.backgroundColor = [UIColor clearColor];
        [gridView addSubview:lineVertical];
    }
    
    for (int i = 0; i < btnCount; i ++) {
        
        NSInteger row= i/4;
        NSInteger col= i%4;
        
        float x = (col+1)*0 + col * (([UIScreen mainScreen].bounds.size.width) / 4);
        CGFloat space = 1;
        if (row == 0) {
            space = 0;
        }else {
            space = 0;
        }
        // btn的起始高度
        float y = (row+1)*space + row * 50;
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.titleLabel.lineBreakMode =  NSLineBreakByTruncatingTail;//省略号在后面
        btn.frame = CGRectMake(x+5, y+3, ((DiaWinWidth) / 4)-10, 50-10-5);
        btn.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 5;
        [btn setTitle:arrTitle[i][@"value"] forState:UIControlStateNormal];
        [btn setTitleColor:WRGBColor(82, 81, 81) forState:UIControlStateNormal];
        
        NSString *strTag;
        //判断是否是写死的排序
//        if (indexPath.section == self.arrHead.count - 1) {
            strTag = [NSString stringWithFormat:@"%zd%zd",indexPath.section + 1,i];
//        }else {
//            strTag = [NSString stringWithFormat:@"%zd90009%d",indexPath.section,i];
//        }
        btn.tag = [strTag integerValue];
        
        [btn addTarget:self action:@selector(btnOfShareClick:) forControlEvents:UIControlEventTouchUpInside];
        //
        //        [btn addTarget:self action:@selector(btnOfShareHightClick:) forControlEvents:UIControlEventTouchDown];
        //
        //        [btn addTarget:self action:@selector(btnOfShareHightClick:) forControlEvents:UIControlEventTouchUpOutside];
        
        [gridView addSubview:btn];
    }
    
    return gridView;
}

- (void)btnOfShareClick:(UIButton *)btn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(btnOfpassValueClicked:)]) {
        [self.delegate btnOfpassValueClicked:btn];
    }
}

@end
